#include <iostream>
#include<unistd.h>
#include<sys/timerfd.h>
#include <stdint.h>
#include<fcntl.h>
int main(){
    // int timerfd_create(int clockid,int flags);
    int timerfd = timerfd_create(CLOCK_MONOTONIC,0);
    if(timerfd < 0){
        perror("timerfd create err");
        return -2;
    }
    // int timerfd_settime(int fd,int flags,struct itimerspec* new,struct itimerspec* old); 
    struct itimerspec itime;
    // 设置第一次超时时间
    itime.it_value.tv_sec = 1;
    itime.it_value.tv_nsec = 0;
    // 超时时间间隔
    // itime.it_interval.tv_sec = 1;
    // itime.it_interval.tv_nsec = 0;
    itime.it_interval = {3,0};
    timerfd_settime(timerfd,0,&itime,nullptr);
    while(true){
        uint64_t times = 0;
        int ret = read(timerfd,&times,sizeof(uint64_t));
        if(ret < 0){
            perror("read err");
            return -1;
        }
        std::cout << "超时了，距离上次超时" << times << "次" <<std::endl;
    }
    close(timerfd);
    return 0;
}